//
//  YCNotificationViewController.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/28.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "YCNotificationViewController.h"
#import "YCNavigationViewController.h"
#import "YCRegisterNoticeViewController.h"
#import "YCScoreNoticeViewController.h"
#import <FXBlurView.h>
#import "YCNotificationCell.h"
#import "YCNotificationModel.h"

@interface YCNotificationViewController ()<UITableViewDelegate,UITableViewDataSource>

/** tableView */
@property (nonatomic, strong) UITableView *tableView;

/** 数据  */
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

#define bgColor [UIColor colorWithWhite:0.2 alpha:0.9]

@implementation YCNotificationViewController

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = bgColor;
    [self makeView];
    [self loadData];
    YCWeakSelf;
    self.tableView.mj_header = [YCRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf loadData];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    YCNavigationViewController *navVc = (YCNavigationViewController *)self.navigationController;
    [navVc setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    YCNavigationViewController *navVc = (YCNavigationViewController *)self.navigationController;
    [navVc setNavigationBarHidden:NO animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)makeView
{
    // 关闭
    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeButton setTitle:@"关闭" forState:UIControlStateNormal];
    [closeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    closeButton.titleLabel.font = [UIFont systemFontOfSize:14];
    closeButton.backgroundColor = [UIColor blackColor];
    [self.view addSubview:closeButton];
    
    // 表
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.yc_width, self.view.yc_height-42) style:UITableViewStylePlain];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.rowHeight = heightFromIphone6Height(120);// tableView的行高
    tableView.backgroundColor = [UIColor clearColor];//-------
    tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
    UINib *nib = [UINib nibWithNibName:NSStringFromClass([YCNotificationCell class]) bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:@"notificationCell"];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.view);
        make.height.equalTo(@42);
    }];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(closeButton.mas_top);
    }];

}

#pragma mark - 数据处理

- (void)loadData
{
    NSDictionary *params = @{
                             @"uid":@"1",
                             @"token":accessToken_Test,
                             };
    [[YCNetworManager shareManager] postWithUrl:YCAPINoticeList params:params success:^(NSURLSessionDataTask *task, ServiceResponse *response) {
        
        self.dataArray = [YCNotificationModel yc_objectArrayWithKeyValuesArray:response.data];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        YCLog(@"%@",error.localizedDescription);
        [self.tableView.mj_header endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCNotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"notificationCell"];
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.itemModel = self.dataArray[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCNotificationModel *model = self.dataArray[indexPath.row];
    UINavigationController *navVc = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    if (model.type == YCNoticeTypeText) {
        YCRegisterNoticeViewController *rnVc = [[YCRegisterNoticeViewController alloc] initWithNid:model.nid];
        [navVc pushViewController:rnVc animated:YES];
    } else if (model.type == YCNoticeTypeScore) {
        YCScoreNoticeViewController *snVc = [[YCScoreNoticeViewController alloc] initWithNid:model];
        [navVc pushViewController:snVc animated:YES];
    }
}



#pragma mark - 点击事件
- (void)close
{
    YCLogFunc
//    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}



@end
